package controller;

import com.google.gson.Gson;
import entry.Book;
import jdk.nashorn.internal.ir.IfNode;
import service.BookService;
import service.impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
@WebServlet("/book/*")
public class bookServlet  extends HttpServlet {
    BookService bookService=new BookServiceImpl();
    Book book=new Book();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获得请求路径
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        String url = request.getRequestURI();
        String methodName = url.substring(url.lastIndexOf("/") + 1);
        Class cla = bookServlet.class;
        try {
            Method method = cla.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(cla.newInstance(), request, response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }
    public void queryAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ArrayList<Book> books = bookService.selectAll();

        Gson gson=new Gson();
        response.getWriter().println(gson.toJson(books));
    }
    public void querybook(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int q =Integer.parseInt(request.getParameter("query")) ;
        String s = request.getParameter("str");
        System.out.println(s);
        if (q==1){
            book.setBOOKNAME(s);
        }else if (q==2){
            book.setBOOKAUTHOR(s);
        }else if (q==3){
            book.setBOOKPUBLISH(s);
        }
        ArrayList<Book> list = bookService.selectBookBy(book);
        Gson gson=new Gson();
        response.getWriter().println(gson.toJson(list));

    }
    public void addBook(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String bname = request.getParameter("bname");
        String aname = request.getParameter("aname");
        System.out.println(aname);
        String cname = request.getParameter("cname");
        int page =Integer.parseInt(request.getParameter("page")) ;
        int price =Integer.parseInt(request.getParameter("price")) ;
        book.setBOOKNAME(bname);
        book.setBOOKAUTHOR(aname);
        book.setBOOKPUBLISH(cname);
        book.setBOOKPAGE(page);
        book.setBOOKPRICE(price);
        boolean b = bookService.addBook(book);
        Gson gson=new Gson();
        response.getWriter().println(gson.toJson(b));

    }
public void delectById(HttpServletRequest request, HttpServletResponse response) throws IOException {
   int bid = Integer.parseInt( request.getParameter("bid"));
    Book book = bookService.selectBid(bid);
    Gson gson=new Gson();
    response.getWriter().println(gson.toJson(book));

}



    }

